package com.chuji.crmcj.act;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chuji.crmcj.R;
import com.chuji.crmcj.adapter.FullPaymentAdapter;
import com.chuji.crmcj.bean.FullPayment;
import com.chuji.crmcj.bean.SureLoseReadInfo;
import com.chuji.crmcj.utils.ConstantValue;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;
import com.chuji.crmcj.utils.VolleyUtil;

/**
 * Created by w.pitt on 2016/1/20.
 * 全款提车
 */
public class FullPayCarAct extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private ListView mLvFullPay;
    //private String salerID;
    private String userID;
    //private FullPayAdapter orderAdapter;
    private FullPaymentAdapter fullPaymentAdapter;
    private FullPayment fullPayment;
    private String tel;
    private String orderInfoID;
    private String name;
    private String carModel;
    private View ll_left;
    /*确认已读*/
    private SureLoseReadInfo sureLoseReadInfo;


    @Override
    protected void initView() {
        setContentView(R.layout.act_full_pay_car);
        mLvFullPay = (ListView) findViewById(R.id.lv_full_pay);
        ll_left = findViewById(R.id.ll_left);
    }

    @Override
    protected void initData() {
        mTvBarTitle.setText("全款提车");
        reqNetData();
    }

    private void reqNetData() {
        userID = SPUtils.getString(UIUtils.getContext(), "UserID", "");
        //userID = "1227FC37-8E05-43ED-BE86-0CE5A401FD6F";
        String strURL = String.format(UrlUtils.FullPayment + "&UserID=%s", userID);
        volleyRequest(strURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.v("FullPayment", s);
                //final FullPayInfo fullPayInfo = JSON.parseObject(s, FullPayInfo.class);
                fullPayment = JSON.parseObject(s, FullPayment.class);
                UIUtils.runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        fullPaymentAdapter = new FullPaymentAdapter(UIUtils.getContext(), mLvFullPay, fullPayment);
                        mLvFullPay.setAdapter(fullPaymentAdapter);
                    }
                });
            }
        });
    }

    @Override
    protected void initListener() {
        mLvFullPay.setOnItemClickListener(this);
        ll_left.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_left:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(this, OrderDetialAct.class);
        intent.putExtra("Lable", ConstantValue.FullPayFragment);
        tel = fullPayment.getApiParamObj().get(i).getCustormerTel();
        orderInfoID = fullPayment.getApiParamObj().get(i).getOrderInfoID();
        name = fullPayment.getApiParamObj().get(i).getCustomerName();
        carModel = fullPayment.getApiParamObj().get(i).getCarModel();
        if(fullPayment.getApiParamObj().get(i).getIsRead().equals("0")){
            SureRead(userID,"全款提车",fullPayment.getApiParamObj().get(i).getOrderInfoID());
        }
        intent.putExtra("CarModel", carModel);
        intent.putExtra("OrderInfoID", orderInfoID);
        intent.putExtra("Name", name);
        intent.putExtra("Tel", tel);
        UIUtils.startActivity(intent);
    }
    //获取读取信息
    public void SureRead(String UserID,String Remark,String BusinessId){
        String strURL = String.format(UrlUtils.SUBMIT_Lose_ISREAD+"&UserID="+UserID+"&Remark="+Remark+"&BusinessId="+BusinessId);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, strURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                sureLoseReadInfo = JSON.parseObject(response,SureLoseReadInfo.class);
                if(sureLoseReadInfo.getApiParamObj().get(0).getIsok().equals("0")){
                    UIUtils.showToastSafe("消息已读");
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                System.out.println("失败" + error);
                UIUtils.showToastSafe("请检查网络");
            }
        });
        // Add the request to the RequestQueue.
        VolleyUtil.getRequestQueue().add(stringRequest);
    }
}
